﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        LuanVanEntities db = new LuanVanEntities();
        public ActionResult MyProducts()
        {
            var model = db.OrderDetails
                .Where(d => d.Order.CustomerId == User.Identity.Name)
                .Select(d => d.Product)
                .Distinct();
            return View("Index", model);
        }
        [HttpPost]
        public ActionResult SearchNhanh(String Name, double Min, double Max)
        {
            var model = db.Products
               .Where(p => p.Name.Contains(Name)
                   && p.UnitPrice >= Min && p.UnitPrice <= Max);
            return View("Index", model);
        }
        public ActionResult Search(String keyword)
        {
            var model = db.Products
                .Where(p => p.Name.Contains(keyword));
            return View("Index", model);
        }
        public ActionResult ListByCategory(int Id)
        {
            var model = db.Products
                .Where(p => p.CategoryId == Id) 
                ;
                
            return View("Index", model);
        }
        public ActionResult ListBySupplier(String Id)
        {
            var model = db.Products
                .Where(p => p.SupplierId == Id);
            return View("Index", model);
        }
        public ActionResult CategoryMenu()
        {
            var model = db.Categories                
               .Where(m => m.Level == 2 && m.Status == true);
            return PartialView("_ChungLoaiSanPham", model);

        }
        public ActionResult NhaCungCapMenu()
        {
            var model = db.Suppliers;
            return PartialView("_NhaCungCap", model);
        }
        public ActionResult ListBySpecial(String Id)
        {
            List<Product> model = null;
            switch (Id)
            {
                case "BAN_CHAY":
                    model = db.Products
                        .Where(p => p.OrderDetails.Count > 0)
                        .OrderByDescending(p => p.OrderDetails.Sum(d => d.UnitPrice * d.Quantity))
                        .Take(8).ToList();
                    break;
                case "MOI":
                    model = db.Products
                        .Where(p => p.Latest == true)
                        .ToList();
                    break;
                case "GIAM_GIA":
                    model = db.Products
                        .Where(p => p.Discount > 0)
                        .OrderByDescending(p => p.Discount)
                        .ToList();
                    break;
                case "YEU_THICH":
                    model = db.Products.ToList();
                    break;
                case "XEM_NHIEU":
                    model = db.Products
                        .Where(p => p.Views > 0)
                        .OrderByDescending(p => p.Views)
                        .ToList();
                    break;
            }
            return View("Index", model);
        }

        public ActionResult Detail(int Id)
        {
            var model = db.Products.Find(Id);
            model.Views++;
            db.SaveChanges();
            //ghi nhân hàng đã xem
            var views = Request.Cookies["Views"];
            if (views == null)
            {
                views = new HttpCookie("Views", Id.ToString());

            }
            if (!views.Value.Contains(Id.ToString()))
            {
                views.Value += "," + Id;
            }
            views.Expires = DateTime.Now.AddMonths(1);
            Request.Cookies.Add(views);
            //truy van cac mat hang da xem
            var ids = views.Value.Split(',')
                .Select(id => int.Parse(id)).ToList();
            ViewBag.Views = db.Products.Where(p => ids.Contains(p.Id));


            return View(model);

        }
        public ActionResult Index()
        {
            var model = db.Products
                .Take(16)
                .OrderByDescending(o=>o.ProductDate);
            return View(model);
        }

	}
}